# -*- coding: utf-8 -*-
"""
Created on 2024-10-08 13:39:57
---------
@summary:
---------
@author: Administrator
"""
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
import time
from util import MyUtils as U
import feapder
from feapder import ArgumentParser
from loguru import logger
from feapder.db.mysqldb import MysqlDB

class Auto(feapder.BatchSpider):
    # 自定义数据库，若项目中有setting.py文件，此自定义可删除
    __custom_setting__ = dict(
        REDISDB_IP_PORTS="localhost:6379",
        # REDISDB_IP_PORTS = "139.9.196.26:6379",
        REDISDB_USER_PASS="python",
        REDISDB_DB=0,
        MYSQL_DB="jfb",
        MYSQL_USER_NAME="py",
        MYSQL_USER_PASS="842jhid5ts#6m8#2",
        COLLECTOR_TASK_COUNT=1,  # 每次获取任务数量，追求速度推荐32
        SPIDER_THREAD_COUNT=1,  # 爬虫并发数，追求速度推荐32
        SPIDER_SLEEP_TIME=0,#下载间隔
        SPIDER_MAX_RETRY_TIMES=1,  # 每个请求最大重试次数
        LOG_LEVEL="INFO"
    )
    _db=None


    @property
    def db(self):
        if not self._db:
            self._db = MysqlDB()
        return self._db
    def start_requests(self, task):
        id=task.id
        choose_year=task.choose_year
        order_id=None
        try:
            order_id=task.order_id
        except:
            order_id=task.order_no
        yield feapder.Request(url="http://139.9.196.26:7001/reportOrder/",auto_request=False,id=id,order_id=order_id,choose_year=choose_year)

    def parse(self, request, response):
        id=request.id
        choose_year=request.choose_year
        order_id=request.order_id
        url=request.url
        tableName=None
        data={}
        if order_id.startswith("qynbsb"):
            url+="reportAnnalsRpa"
            tableName="annals_order"
        elif order_id.startswith("qynb"):
            url+="entReportRpa"
            tableName="qiyenianbao_order"
        elif order_id.startswith("gsnb"):
            url+="gsreportRpa"
            tableName="gongshangnianbao_order"
        elif order_id.startswith("hsnb"):
            url+="red_reportRpa"
            tableName="qiyenianbao_order_five"
        elif order_id.startswith("nbxt"):
            url+="annual_statementRpa"
            tableName="nianbaoxitong_order"
        elif order_id.startswith("gtns"):
            url+="gtnsreportRpa"
            tableName="nianbaoshenbao_order"
        elif order_id.startswith("gtnb"):
            url+="gtreportRpa"
            tableName="getinianbao_order"
        else:
            yield self.update_task_batch(request.id, autoState=-1)
        data["order_id"]=order_id
        data["choose_year"]=choose_year
        data["company_person_id"]="683"
        headers = {
            "sign": "b093de76b725c670efc53e09af930f7e"
        }
        logger.info(tableName)
        logger.info(data)
        logger.info(url)
        yield feapder.Request(url=url, data=U.json_dumps(data), id=id,tableName=tableName,headers=headers,callback=self.ok)

    def ok(self, request, response:feapder.Response):
        logger.info(response.text)
        if response.text.startswith('{"detail":[{"loc":["body"],"msg":"value is not a valid dict","type":"type_error.dic'):
            logger.info("有问题")
        self.db.update_smart(request.tableName, {"autoState": -1}, condition=f"id={request.id}")
if __name__ == "__main__":
    # 个体年报
    getinianbao_order = Auto( #qynb
        redis_key="autoTask:getinianbao_order",  # 分布式爬虫调度信息存储位置
        task_table="getinianbao_order",  # mysql中的任务表
        task_keys=["id", "order_no","choose_year"],  # 需要获取任务表里的字段名，可添加多个
        task_state="autoState",  # mysql中任务状态字段
        task_condition="status=2 and  created_at >='2024-10-21' and province is not null and  province!=''",
        batch_record_table="getinianbao_order_batch_record",  # mysql中的批次记录表
        batch_name="个体年报",  # 批次名字
        batch_interval=1/24/60,  # 批次周期 天为单位 若为小时 可写 1 / 24
        task_limit=1,
        min_task_count=1,
        check_task_interval=30,
        keep_alive=True
    )
    # 个体年审
    nianbaoshenbao_order = Auto( #qynb
        redis_key="autoTask:nianbaoshenbao_order",  # 分布式爬虫调度信息存储位置
        task_table="nianbaoshenbao_order",  # mysql中的任务表
        task_keys=["id", "order_no","choose_year"],  # 需要获取任务表里的字段名，可添加多个
        task_state="autoState",  # mysql中任务状态字段
        task_condition="status=2 and  created_at >='2024-10-16' and province is not null and  province!=''",
        batch_record_table="nianbaoshenbao_order_batch_record",  # mysql中的批次记录表
        batch_name="个体年审",  # 批次名字
        batch_interval=1/24/60,  # 批次周期 天为单位 若为小时 可写 1 / 24
        task_limit=1,
        min_task_count=1,
        check_task_interval=30,
        keep_alive=True
    )
    # 企业年报
    qiyenianbao_order = Auto( #qynb
        redis_key="autoTask:qiyenianbao_order",  # 分布式爬虫调度信息存储位置
        task_table="qiyenianbao_order",  # mysql中的任务表
        task_keys=["id", "order_no","choose_year"],  # 需要获取任务表里的字段名，可添加多个
        task_state="autoState",  # mysql中任务状态字段
        task_condition="status=2 and  created_at >='2024-10-09' and province is not null and  province!=''",
        batch_record_table="qiyenianbao_order_batch_record",  # mysql中的批次记录表
        batch_name="企业年报",  # 批次名字
        batch_interval=1/24/60,  # 批次周期 天为单位 若为小时 可写 1 / 24
        task_limit=1,
        min_task_count=1,
        check_task_interval=30,
        keep_alive=True

    )

    # 年报年审
    annals_order = Auto(  #qynbsb
        redis_key="autoTask:annals_order111dsddddw1",  # 分布式爬虫调度信息存储位置
        task_table="annals_order",  # mysql中的任务表
        task_keys=["id","order_id", "choose_year"],  # 需要获取任务表里的字段名，可添加多个
        task_state="autoState",  # mysql中任务状态字段
        task_condition="status=2 and  create_time >='2024-10-09' and province is not null and  province!=''",
        batch_record_table="annals_order_batch_record",  # mysql中的批次记录表
        batch_name="年报年审",  # 批次名字
        batch_interval=1/24/60 , # 批次周期 天为单位 若为小时 可写 1 / 24
        task_limit = 1,
        min_task_count = 1,
        check_task_interval =  30,
        keep_alive=True
    )

    # # 工商年报
    gongshangnianbao_order = Auto( #gsnb
        redis_key="autoTask:gongshangnianbao_order1",  # 分布式爬虫调度信息存储位置
        task_table="gongshangnianbao_order",  # mysql中的任务表
        task_keys=["id","order_no","choose_year"],  # 需要获取任务表里的字段名，可添加多个
        task_state="autoState",  # mysql中任务状态字段
        task_condition="status=2 and  created_at >='2024-10-09' and province is not null and  province!=''",
        batch_record_table="gongshangnianbao_order_batch_record",  # mysql中的批次记录表
        batch_name="工商年报",  # 批次名字
        batch_interval=1/24/60,  # 批次周期 天为单位 若为小时 可写 1 / 24
        task_limit=1,
        min_task_count=1,
        check_task_interval=30,
        keep_alive=True

    )

    # 红色年报
    qiyenianbao_order_five = Auto( #hsnb
        redis_key="autoTask:gongshangnianbao_order",  # 分布式爬虫调度信息存储位置
        task_table="qiyenianbao_order_five",  # mysql中的任务表
        task_keys=["id","order_no", "choose_year"],  # 需要获取任务表里的字段名，可添加多个
        task_state="autoState",  # mysql中任务状态字段
        task_condition="status=2 and  created_at >='2024-10-09' and province is not null and  province!=''",
        batch_record_table="qiyenianbao_order_five_batch_record",  # mysql中的批次记录表
        batch_name="红色年报",  # 批次名字
        batch_interval=1/24/60,  # 批次周期 天为单位 若为小时 可写 1 / 24
        task_limit=1,
        min_task_count=1,
        check_task_interval=30,
        keep_alive=True
    )

    # 年报系统
    nianbaoxitong_order = Auto( #nbxt
        redis_key="autoTask:nianbaoxitong_order",  # 分布式爬虫调度信息存储位置
        task_table="nianbaoxitong_order",  # mysql中的任务表
        task_keys=["id", "order_no","choose_year"],  # 需要获取任务表里的字段名，可添加多个
        task_state="autoState",  # mysql中任务状态字段
        task_condition="status=2 and  created_at >='2024-10-09' and province is not null and  province!=''",
        batch_record_table="nianbaoxitong_order_batch_record",  # mysql中的批次记录表
        batch_name="年报系统",  # 批次名字
        batch_interval=1/24/60,  # 批次周期 天为单位 若为小时 可写 1 / 24
        task_limit=1,
        min_task_count=1,
        check_task_interval=30,
        keep_alive=True
    )

    parser = ArgumentParser(description="Auto爬虫")

    parser.add_argument("--task1",action="store_true",help="企业年报添加任务",function=qiyenianbao_order.start_monitor_task)
    parser.add_argument("--start1",action="store_true", help="企业年报启动爬虫", function=qiyenianbao_order.start)

    parser.add_argument("--task2",action="store_true",help="年报年审添加任务",function=annals_order.start_monitor_task)
    parser.add_argument("--start2",action="store_true",help="年报年审添加任务",function=annals_order.start)

    parser.add_argument("--task3",action="store_true",help="工商年报添加任务",function=gongshangnianbao_order.start_monitor_task)
    parser.add_argument("--start3",action="store_true", help="工商年报启动爬虫", function=gongshangnianbao_order.start)

    parser.add_argument("--task4",action="store_true",help="红色年报添加任务",function=qiyenianbao_order_five.start_monitor_task)
    parser.add_argument("--start4",action="store_true", help="红色年报启动爬虫", function=qiyenianbao_order_five.start)

    parser.add_argument("--task5",action="store_true",help="年报系统添加任务",function=nianbaoxitong_order.start_monitor_task)
    parser.add_argument("--start5",action="store_true", help="年报系统启动爬虫", function=nianbaoxitong_order.start)


    parser.add_argument("--task6",action="store_true",help="个体年审添加任务",function=nianbaoshenbao_order.start_monitor_task)
    parser.add_argument("--start6",action="store_true", help="个体年审启动爬虫", function=nianbaoshenbao_order.start)

    parser.add_argument("--task7",action="store_true",help="个体年报添加任务",function=getinianbao_order.start_monitor_task)
    parser.add_argument("--start7",action="store_true", help="个体年报启动爬虫", function=getinianbao_order.start)

    parser.start()
